package com.example.config;

import com.example.constant.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "topic");
        return new CustomExchange(MqConstants.Exchange.ORDER_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue delayedQueue() {
        return new Queue(MqConstants.Queue.ORDER_DELAY_QUEUE, true);
    }

    @Bean
    public Binding delayedBinding(Queue delayedQueue, CustomExchange delayedExchange) {
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(MqConstants.Key.ORDER_DELAY_KEY).noargs();
    }

    @Bean
    public Queue refundQueue() {
        return new Queue(MqConstants.Queue.REFUND_DELAY_QUEUE, true);
    }

    @Bean
    public Binding refundBinding(Queue refundQueue, CustomExchange delayedExchange) {
        return BindingBuilder.bind(refundQueue).to(delayedExchange).with(MqConstants.Key.ORDER_REFUND_KEY).noargs();
    }
}
